<?php


namespace app\login\service;


use app\lib\exception\ParameterException;
use Driver\Cache\Redis;
use think\Exception;

class ShowService
{
    # 通过name获取所有的标识
    public function getSign($name)
    {
        $redis_hash_key = config('redis.account').$name;
        $sign_redis = Redis::hget($redis_hash_key);
        $sign = null;
        if(count($sign_redis) !== 0 || array_key_exists('id', $sign_redis) == true){
            $sign = $sign_redis;
        }else{
            try{
                $sign_mysql = self::where('name', '=', $name)
                    ->find()
                    ->toArray();
                $sign = $sign_mysql;
            }catch (Exception $e){
                throw new ParameterException([
                    'msg' => '尝试获取的name不存在'
                ]);
            }
        }
        $host = $this->getHost($sign);
        $sign_arr = explode('%', $sign['sign']);
        return [
            'arr' => $sign_arr,
            'info' => $sign,
            'host_d' => $host['d'],
            'host_f' => $host['f'],
        ];
    }

    private function getHost($sign)
    {
        global $host_d;
        global $host_f;
        if($sign['host_d'] == '' || $sign['host_d'] == null){
            $host_d = false;
        }else{
            $host_d = true;
        }
        if($sign['host_f'] == '' || $sign['host_f'] == null){
            $host_f = false;
        }
        else{
            $host_f = true;
        }
        return [
            'd' => $host_d,
            'f' => $host_f
        ];
    }
}